package com.wanmait.schedule.interceptor;

import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.wanmait.schedule.entity.Student;
import com.wanmait.schedule.entity.Teacher;
import com.wanmait.schedule.service.StudentService;
import com.wanmait.schedule.service.TeacherService;
import com.wanmait.schedule.util.JWTUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//拦截器
@Component
public class JWTInterceptor implements HandlerInterceptor {


/*    @Value("${jwt.secret}")
    private String secret;*/

    @Resource
    private StudentService studentService;
    @Resource
    private TeacherService teacherService;

    //调用功能处理方法之前执行，返回true继续执行，返回false表示不会继续执行功能处理方法
    //返回false如果前后端不分离一般配合重定向，如果前后端分离一般就直接返回消息
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //如果是预检请求，直接通过
        String method = request.getMethod();
        if("OPTIONS".equals(method)){
            return true;
        }
        //请求的为控制器的方法时才验证token
        if(!(handler instanceof HandlerMethod)){
            return true;
        }

        //获得token
        String token = request.getHeader("token");
        if(token==null){
            throw new JWTVerificationException("没有携带token令牌");
        }
        //获得签发对象
        Integer id = null;
        try {
            id = Integer.parseInt(JWTUtils.getAudience(token));
        } catch (NumberFormatException e) {
            throw new JWTDecodeException("签发对象解析失败");
        }
        //获得admin
        Student student = studentService.getById(id);
        Teacher teacher = teacherService.getById(id);
        if(student == null){
            if(teacher == null){
                throw new JWTVerificationException("没找到用户");
            }
            JWTUtils.verifyToken(token,teacher.getPassword());
            request.setAttribute("teacher",teacher);
        }
        JWTUtils.verifyToken(token,student.getPassword());
        request.setAttribute("student",student);
//        Admin admin = adminService.getById(id);
//        if(admin==null){
//            throw new JWTVerificationException("没找到管理员");
//        }
//        //验证token是否正确
//        JWTUtils.verifyToken(token,admin.getPassword());

        //token验证通过
//        request.setAttribute("admin",admin);
        return true;
    }

/*    //功能处理方法之后执行,视图渲染之前，可以对ModelAndView进行处理
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle..............");
    }
    //功能处理方法之后执行,视图渲染完成
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion..............");
    }*/
}
